'use strict';

var async = require('async');
var moment = require('moment');
var mgrUtil = require('../util/mgrUtil');
var actType = require('../consts/activityType.js');
var eventType = require('../consts/eventType.js');
var playerManager = require('../libs/playerManager.js');
var configUtil = require('../util/configUtil');
var activityConf = require('../config/activityConf');
var buildType = require('../consts/buildType');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil');
var openServerConf = require('../config/openServerConf.js');
var _ = require('lodash');
var openTime = require('../../config/development/open.json');

exports.push = function (role_id, activity_type, player_manager, cb) {
    _getActivityStatus(role_id, activity_type, player_manager, function (err, act_status) {
        if (!!err) {
            console.error(err);
            return;
        }

        mgrUtil.sendStatusMessage(
           [role_id],
            eventType.ON_ACTIVITY_STATUS_CHANGE,
            act_status,
            cb
        );
    });
};

var _getActivityStatus = function (role_id, activity_type, player_manager, callback) {
    var act_list = [
            actType.CHECK,
            actType.OPEN,
            actType.SEVEN_LOGIN,
            actType.GROW,
            actType.MAIN_CITY,

            // 七天狂欢类型
            actType.HERO_LV,
            actType.COPY_STAR,
            actType.NEXUS_LV,
            actType.BATTLE_NUM,
            actType.RANK,
            actType.DEFENSE,
            actType.SOLDIER_STAR
            /////////////////////////////
            ];

    var map = {};
    map[actType.CHECK] = _getCheckStatus;
    map[actType.OPEN] = _getOpenStatus;
    map[actType.SEVEN_LOGIN] = _getSevenLoginStatus;
    map[actType.GROW] = _getGrowStatus;
    map[actType.MAIN_CITY] = _getMainCityStatus;

    map[actType.HERO_LV] = _getHeroLvStatus;
    map[actType.COPY_STAR] = _getCopyStarStatus;
    map[actType.NEXUS_LV] = _getNexusLvStatus;
    map[actType.BATTLE_NUM] = _getBattleNumStatus;
    map[actType.RANK] = _getRankStatus;
    map[actType.DEFENSE] = _getDefenseStatus;
    map[actType.SOLDIER_STAR] = _getSoldierStarStatus;

    if (!player_manager) {
        player_manager = new playerManager(role_id);
    }
    var act_status = [];
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(
                    ['check', 'role', 'build', 'activity', 'copy', 'task', 'battle', 'soldier', 'accrued'],
                    cb
                );
            },
            function (cb) {
                async.eachSeries(act_list, function (act, cb1) {
                        var status = map[act](player_manager);
                        var activity_type = parseInt(act);
                        act_status.push({
                            activity_type: activity_type,
                            status: status
                        });
                        cb1();
                    },
                    function (err) {
                        if (!!err) {
                            console.error(err);
                            return cb(err);
                        }
                        cb();
                    });
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback(err);
            }

            callback(null, {
                activity_status: act_status
            });
        }
    );
};

// 当天未签到
var _getCheckStatus = function (player_manager) {
    var check_model = player_manager.check;
    var role_model = player_manager.role;
    var pre_check_day = check_model.getPreCheckDay() || 0;
    var curr_day = parseInt(moment().format('D'));

    var check_month = parseInt(moment().format('M'));
    var job_type = role_model.getJob();
    var key = job_type * 100 + check_month;
    var check_conf = dataApi.check.findById(key);
    var max_day = check_conf.length;

    if (pre_check_day < curr_day && pre_check_day < max_day) {
        return 1;
    }
    return 0;
};

// 首测送礼(到开服第n天有未领取的奖励)
var _getOpenStatus = function (player_manager) {
    var activity_model = player_manager.activity;
    var role_model = player_manager.role;

    var open_time = moment.unix(configUtil.load('open').open_time);
    open_time = moment(open_time).startOf('day');
    var day = parseInt(moment().diff(moment(open_time), 'd') + 1);
    var end_days = constantUtil.value('open_service_time');

    if (day > end_days) {
        return 0;
    }

    var total_days = dataApi.open_service.length();
    if (day > total_days) {
        day = total_days;
    }

    for (var i = 1; i <= day; i++) {
        if (activity_model.openServerRewardIsCanReceive(i)) {
            return 1;
        }
    }

    return 0;
};

// 七天登录(到创角第n天有未领取的奖励)
var _getSevenLoginStatus = function (player_manager) {
    var role_model = player_manager.role;
    var activity_model = player_manager.activity;

    // 创角第几天
    var create_time = role_model.create_time;
    var current_day = moment().startOf('day').diff(moment(create_time).startOf('day'), 'd') + 1;
    var day_list = activityConf.getSevenLoginLevelList();
    if (current_day > day_list.length) {
        return 0;
    }

    // 连续登录天数
    var accumulative_days = activity_model.getContinuouslyLoginDays();
    if (current_day > accumulative_days) {
        current_day = accumulative_days;
    }

    for (var i = 1; i <= current_day; i++) {
        if (activity_model.sevenLoginRewardIsCanReceive(i)) {
            return 1;
        }
    }

    return 0;
};

// 英雄等级奖励(到角色当前等级，有未领取的奖励)
var _getGrowStatus = function (player_manager) {
    var activity_model = player_manager.activity;
    var role_model = player_manager.role;

    var role_lv = role_model.getLv();
    var lvl_list = activityConf.getGrowUpLvlList();

    // 获取活动列表
    for (var i = 0; i <= lvl_list.length - 1; i++) {
        var reward_lv = lvl_list[i];
        if (role_lv < reward_lv) {
            return 0;
        }
        if (activity_model.growUpRewardIsCanReceive(reward_lv)) {
            return 1;
        }
    }
    return 0;
};

// 主城等级奖励(到主城当前等级，有未领取的奖励)
var _getMainCityStatus = function (player_manager) {
    var activity_model = player_manager.activity;
    var build_model = player_manager.build;
    var nexus_build = build_model.getBuildByType(buildType.NEXUS);
    var nexus_lv = nexus_build.getLv();
    var lvl_list = activityConf.getMainCityLevelList();

    // 获取活动列表
    for (var i = 0; i <= lvl_list.length - 1; i++) {
        var reward_lv = lvl_list[i];
        if (nexus_lv < reward_lv) {
            return 0;
        }

        if (activity_model.mainCityUpRewardIsCanReceive(reward_lv)) {
            return 1;
        }
    }
    return 0;
};

// 英雄等级奖励(开服7天狂欢 --- 到角色当前等级，有未领取的奖励)
var _getHeroLvStatus = function (player_manager) {
    var now_open_time = openTime.open_time;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var role_model = player_manager.role;
        var role_lv = role_model.getLv();
        var lvl_list = openServerConf.getValueList(1);
        // 获取活动列表
        for (var i = 0; i <= lvl_list.length - 1; i++) {
            var reward_lv = lvl_list[i];
            if (role_lv < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(1, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};

// 主城等级奖励(开服7天狂欢 --- 到主城当前等级，有未领取的奖励)
var _getNexusLvStatus = function (player_manager) {
    var now_open_time = openTime.open_time + 2 * 24 * 3600;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var build_model = player_manager.build;
        var nexus_build = build_model.getBuildByType(buildType.NEXUS);
        var nexus_lv = nexus_build.getLv();
        var lvl_list = openServerConf.getValueList(3);

        // 获取活动列表
        for (var i = 0; i <= lvl_list.length - 1; i++) {
            var reward_lv = lvl_list[i];
            if (nexus_lv < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(3, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};

// 副本星级奖励(开服7天狂欢 --- 到副本星级，有未领取的奖励)
var _getCopyStarStatus = function (player_manager) {
    var now_open_time = openTime.open_time + 24 * 3600;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var copy_model = player_manager.copy;
        var copy_total_star = 0;
        var copy_list = dataApi.carbon_map.all();
        _.each(copy_list, function (copy_id, copy_key) {
            copy_total_star += copy_model.getTotalStarNum(copy_key);
        });
        var start_list = openServerConf.getValueList(2);

        // 获取活动列表
        for (var i = 0; i <= start_list.length - 1; i++) {
            var reward_lv = start_list[i];
            if (copy_total_star < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(2, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};
//////////////////////////////////////////////////////////
// 掠夺奖励(开服7天狂欢 --- 有未领取的奖励)
var _getBattleNumStatus = function (player_manager) {
    var now_open_time = openTime.open_time + 3 * 24 * 3600;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var task_model = player_manager.task;
        var battle_num = task_model.getStaticList(217).battle_success_num || 0;
        var battle_list = openServerConf.getValueList(4);

        // 获取活动列表
        for (var i = 0; i <= battle_list.length - 1; i++) {
            var reward_lv = battle_list[i];

            if (battle_num < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(4, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};

// 官阶奖励(开服7天狂欢 --- ，有未领取的奖励)
var _getRankStatus = function (player_manager) {
    var now_open_time = openTime.open_time + 4 * 24 * 3600;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var battle_model = player_manager.battle;
        var cup_value = battle_model.getNoble();
        var cup_list = openServerConf.getValueList(5);

        // 获取活动列表
        for (var i = 0; i <= cup_list.length - 1; i++) {
            var reward_lv = cup_list[i];
            if (cup_value < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(5, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};

// 防守奖励(开服7天狂欢 --- 有未领取的奖励)
var _getDefenseStatus = function (player_manager) {
    var now_open_time = openTime.open_time + 5 * 24 * 3600;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var task_model = player_manager.task;
        var battle_num = task_model.getStaticList(218).battle_defense_num || 0;
        var start_list = openServerConf.getValueList(6);

        // 获取活动列表
        for (var i = 0; i <= start_list.length - 1; i++) {
            var reward_lv = start_list[i];
            if (battle_num < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(6, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};

// 士兵星级奖励(开服7天狂欢 --- 到副本星级，有未领取的奖励)
var _getSoldierStarStatus = function (player_manager) {
    var now_open_time = openTime.open_time + 6 * 24 * 3600;
    var end_open_time = openTime.open_time + 7 * 24 * 3600;
    var now_time = moment().unix();
    var is_open = false;
    if (now_time >= now_open_time && now_time <= end_open_time) {
        is_open = true;
    }

    if (!!is_open) {
        var activity_model = player_manager.activity;
        var soldier_model = player_manager.soldier;

        // 对应星级
        var soldier_start_list1 = openServerConf.getValueList1(7)[0];

        // 星级
        var soldier_start_list = openServerConf.getValueList(7);

        // 获取活动列表
        for (var i = 0; i <= soldier_start_list.length - 1; i++) {
            var reward_lv = soldier_start_list[i];

            var start_num = soldier_model.getSoldierStarNum1(soldier_start_list1);

            if (start_num < reward_lv) {
                return 0;
            }

            var carnival_id = openServerConf.getTypeByValue4(7, reward_lv);
            if (activity_model.isCarnivalReceive(carnival_id)) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
};
